import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)

const routes = [
  // 路由重定向
  { path: '*', name: 'redirect', redirect: '/my', },
  {
    path: '/login',
    name: 'login',
    component: () => import('../views/login/index'),
    meta: { title: '登录' }
  },
  {
    path: '/reg/:keys',
    name: 'reg',
    component: () => import('../views/reg/index'),
    meta: { title: '注册' },
  },
  {
    path: '/home',
    name: 'home',
    component: () => import('../views/home/index'),
    meta: { title: '冻眠市场系统' }
  },
  {
    path: '/shoplist/:id',
    name: 'shoplist',
    component: () => import('../views/shop/shopList'),
    meta: { title: '更多' },
  },
  {
    path: '/shop',
    name: 'shop',
    component: () => import('../views/shop/index'),
    meta: { title: '店铺信息' },
  },
  {
    path: '/goods',
    name: 'goods',
    component: () => import('../views/goods/index'),
    meta: { title: '商品管理' },
  },
  {
    path: '/addgoods',
    name: 'addgoods',
    component: () => import('../views/goods/addgoods'),
    meta: { title: '添加商品' },
  },
  {
    path: '/addtype',
    name: 'addtype',
    component: () => import('../views/goods/addType'),
    meta: { title: '添加商品类型' },
  },
  {
    path: '/edittype',
    name: 'edittype',
    component: () => import('../views/goods/editType'),
    meta: { title: '编辑商品类型' },
  },
  {
    path: '/goodsdetail',
    name: 'goodsdetail',
    component: () => import('../views/goods/GoodsDetail'),
    meta: { title: '商品详情' },
  },
  {
    path: '/facility',
    name: 'facility',
    component: () => import('../views/facility/index'),
    meta: { title: '设备列表' },
  },
  {
    path: '/addfacility',
    name: 'addfacility',
    component: () => import('../views/facility/add'),
    meta: { title: '添加设备' },
  },
  {
    path: '/facilitydetail',
    name: 'facilitydetail',
    component: () => import('../views/facility/detail'),
    meta: { title: '设备详情' },
  },
  {
    path: '/sends/:active',
    name: 'sends',
    component: () => import('../views/send/index'),
    meta: { title: '订单管理' },
  },
  {
    path: '/orderDetail/:sid',
    name: 'orderDetail',
    component: () => import('../views/send/orderDetail'),
    meta: { title: '订单详情' },
  },
  {
    path: '/historys',
    name: 'historys',
    component: () => import('../views/buyhistory/index'),
    meta: { title: '我的购买记录' },
  },
  {
    path: '/historysDetail/:sid',
    name: 'historysDetail',
    component: () => import('../views/buyhistory/orderDetail'),
    meta: { title: '购买记录详情' },
  },
  {
    path: '/classify',
    name: 'classify',
    component: () => import('../views/classify/index'),
    meta: { title: '商品管理' },
  },
  {
    path: '/controls',
    name: 'controls',
    component: () => import('../views/controls/NavBar'),
    meta: { title: '管理中心' },
  },
  {
    path: '/my',
    name: 'my',
    component: () => import('../views/my/index'),
    meta: { title: '个人中心' },
  },
  {
    path: '/addresss',
    name: 'addresss',
    component: () => import('../views/my/address'),
    meta: { title: '我的地址' },
  },
  {
    path: '/message',
    name: 'message',
    component: () => import('../views/message/index'),
    meta: { title: '消息管理' },
  },
  {
    path: '/buy',
    name: 'buy',
    component: () => import('../views/buy/index'),
    meta: { title: '下单' }, // 普通下单
  },
  {
    path: '/placeorder',
    name: 'placeorder',
    component: () => import('../views/buy/placeOrder'),
    meta: { title: '结算' }, // 普通下单
  },
  {
    path: '/regbuy',
    name: 'regbuy',
    component: () => import('../views/buy/regindex'),
    meta: { title: '注册下单' }, // 注册下单
  },
  {
    path: '/regplaceorder',
    name: 'regplaceorder',
    component: () => import('../views/buy/regplaceOrder'),
    meta: { title: '结算' }, // 注册下单
  },
  {
    path: '/passenger',
    name: 'passenger',
    component: () => import('../views/passenger/index'),
    meta: { title: '客源管理' },
  },
  {
    path: '/customerDetail',
    name: 'customerDetail',
    component: () => import('../views/passenger/detail'),
    meta: { title: '客源管理' },
  },
  {
    path: '/financeA',
    name: 'financeA',
    component: () => import('../views/financeA/index'),
    meta: { title: '财务数据' },
  },
  {
    path: '/revenuesA',
    name: 'revenuesA',
    component: () => import('../views/financeA/revenues'),
    meta: { title: '店铺财务数据' },
  },
  {
    path: '/reportsB',
    name: 'reportsB',
    component: () => import('../views/financeB/reports'),
    meta: { title: '收款记录' },
  },
  {
    path: '/revenuesB',
    name: 'revenuesB',
    component: () => import('../views/financeB/revenues'),
    meta: { title: '经营报表' },
  },
  {
    path: '/join',
    name: 'join',
    component: () => import('../views/Join/JoinRequest'),
    meta: { title: '审批列表' },
  },
  {
    path: '/joinDetail',
    name: 'joinDetail',
    component: () => import('../views/Join/JoinRequestDetail'),
    meta: { title: '审批详情' },
  },
  {
    path: '/testing',
    name: 'testing',
    component: () => import('../views/goodsTesting/Testing'),
    meta: { title: '商品质检' },
  },
  {
    path: '/testingDetail',
    name: 'testingDetail',
    component: () => import('../views/goodsTesting/TestingDetail'),
    meta: { title: '质检详情' },
  },
  {
    path: '/visitor',
    name: 'visitor',
    component: () => import('../views/visitor/index'),
    meta: { title: '冻眠工厂参观预约登记表' },
  },
]

const router = new VueRouter({
  routes
})

//路由守卫
router.beforeEach((to, from, next) => {
  //获取登录存储的数据
  let userInfo = localStorage.getItem('userInfo');
  //如果当前进入页面是登录或注册就不执行任何操作，继续进入。	
  if (to.name === 'login' || to.name === 'reg' || to.name === 'visitor') {
    next();
  } else {
    //否则判断是否存在登录存储的数据，不存在就进入登录页面
    userInfo === null ? next({ name: 'login' }) : next()
  }
  if (to.name === 'login') {
    //手动进入登录页面自动清空用户信息，防止手动修改url跳转
    window.localStorage.clear()
  }
});

export default router
